package com.leyou.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entitry.Brand;
import com.leyou.item.entitry.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 张伟奇
 * @Date 2021/7/15 11:49
 * @description
 * @Version 1.0
 **/
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
   @Autowired
   private BrandMapper brandMapper;

    @Override
    public List<BrandDTO> findBrandById(Long id) {
        List<Brand> brandById = this.brandMapper.selectList(new QueryWrapper<Brand>().eq("id", id));
        return BrandDTO.converEntity(brandById);
    }

    @Override
    public List<BrandDTO> findBrandByCategoryId(Long id) {
        return brandMapper.findBrandByCategoryId(id);
    }

    @Override
    public List<BrandDTO> findBrandIds(List<Integer> ids) {
        List<Brand> brands = brandMapper.selectBatchIds(ids);

        return BrandDTO.converEntity(brands);
    }

    @Override
    public PageDTO<BrandDTO> findBrandByPage(Integer page, Integer rows, String key) {
        page=Math.min(page,100);
        rows=Math.max(rows,500);

        Page<Brand> brandPage = new Page<>(page, rows);
        boolean isKeyExists = StringUtils.isNotBlank(key);
        query().like(isKeyExists,"name",key)
                .or()
                .eq(isKeyExists,"letter",key)
                .page(brandPage);
        List<Brand> brandList = brandPage.getRecords();
        return  new PageDTO<>(brandPage.getTotal(),brandPage.getPages(),BrandDTO.converEntity(brandList));
    }

    @Override
    @Transactional()
    public void saveBrand(Brand  brand,List<Long> categoryIds) {
        this.brandMapper.insert(brand);
        Long brandId = brand.getId();
        for (Long categoryId : categoryIds) {
            this.brandMapper.insertCategoryIdBrandId(categoryId,brandId);
        }

    }

    @Override
    @Transactional
    public void updateBrand(Brand brand, List<Long> categoryIds) {

        this.brandMapper.updateBrand(brand);
        Long brandId = brand.getId();
        this.brandMapper.deleteCategoryIdBrandId(brandId);
        for (Long categoryId : categoryIds) {
            this.brandMapper.insertCategoryIdBrandId(categoryId,brandId);
        }

    }


    @Override
    @Transactional
    public void deleteBrandId(Long id) {
         this.brandMapper.deleteCategoryBrand(id);
         this.remove(new QueryWrapper<Brand>().eq("id", id));
    }


}
